<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="统计时间" prop="dateRange">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['manage:teacher:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="statisticsList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" type="index" width="50" align="center" />
      <el-table-column label="老师" prop="teacherName" width="100" align="center" />
      <el-table-column label="班级" prop="className" width="120" align="center" />
      <el-table-column label="班级编码" prop="classCode" width="100" align="center" />
      <el-table-column label="学生人数" prop="studentCount" width="80" align="center" />
      <el-table-column label="总提交记录" prop="totalSubmissions" width="100" align="center" />
      <el-table-column label="已打卡人次" prop="submittedCount" width="100" align="center">
        <template slot-scope="scope">
          <span class="text-success">{{ scope.row.submittedCount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="未打卡人次" prop="unsubmittedCount" width="100" align="center">
        <template slot-scope="scope">
          <span class="text-danger">{{ scope.row.unsubmittedCount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="打卡率" prop="attendanceRate" width="100" align="center">
        <template slot-scope="scope">
          <el-tag :type="getAttendanceRateType(scope.row.attendanceRate)">
            {{ scope.row.attendanceRate }}%
          </el-tag>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getTeacherStatistics } from "@/api/manage/teacher";

export default {
  name: "TeacherStatistics",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教师统计表格数据
      statisticsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        startDate: null,
        endDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    // 默认查询最近30天的数据
    const endDate = new Date();
    const startDate = new Date();
    startDate.setDate(startDate.getDate() - 30);
    this.dateRange = [this.parseTime(startDate, '{y}-{m}-{d}'), this.parseTime(endDate, '{y}-{m}-{d}')];
    this.getList();
  },
  methods: {
    /** 查询教师统计列表 */
    getList() {
      this.loading = true;
      this.queryParams.startDate = null;
      this.queryParams.endDate = null;
      if (null != this.dateRange && '' != this.dateRange) {
        this.queryParams.startDate = this.dateRange[0];
        this.queryParams.endDate = this.dateRange[1];
      }
      getTeacherStatistics(this.queryParams).then(response => {
        this.statisticsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        teacherId: null,
        teacherNo: null,
        teacherName: null,
        sex: null,
        phone: null,
        email: null,
        entryTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.teacherId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('manage/teacher/export', {
        ...this.queryParams
      }, `teacher_statistics_${new Date().getTime()}.xlsx`)
    },
    /** 根据打卡率获取标签类型 */
    getAttendanceRateType(rate) {
      if (rate >= 95) return 'success';
      if (rate >= 80) return 'warning';
      return 'danger';
    }
  }
};
</script>

<style scoped>
.text-success {
  color: #67c23a;
}
.text-danger {
  color: #f56c6c;
}
</style>